<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
	 /**
     * 管理员列表
     */
    public function index() {
        //使用D调用视图模型
        $user = D('UserView');
        $data = $user->order('id desc')->select();
        $this->assign('data',$data);
				//var_dump($data);
        $this->display();
    }
    
    /**
     * 新增管理员
     */
    public function add() {
        if(IS_POST) {
            $data['pwdid'] = I('post.pwdid',0,'int');
						$data['xid'] = I('post.xid',0,'int');
            $data['name'] = I('post.name','','addslashes');
            $data['password'] = md5(I('post.pwd','','addslashes'));
						$data['quit_id'] = I('post.quit_id',1,'int');
            //通过D实例化模型类
            $user = D('User');
            if($user->create()) {
                if($user->add($data)) {
                    $this->success('新增用户成功',U('User/index'));
                }else {
                    $this->error('新增用户失败',U('User/index'));
                }
            }else {
                $this->error($user->getError());
            }
        }else {
            //添加管理员之前 查询出已有的角色
						$campus = M('other')->where('pid=1')->select();
						$this->assign('campus',$campus);
            $power = M('power');
            $power_data = $power->select();
            $this->assign('power_data',$power_data);
            $this->display();
        }
    }
    
    /**
     * 编辑管理员
     */
    public function edit() {
        //通过D实例化模型类
        $user = D('User');
        if(IS_POST) {
            $id = I('post.id',0,'int');
            $data['pwdid'] = I('post.pwdid',0,'int');
						$data['xid'] = I('post.xid',0,'int');
            $data['name'] = I('post.name','','addslashes');
            $pwd = I('post.pwd','','addslashes');
						$data['quit_id'] = I('post.quit_id',1,'int');
            if($pwd !='') {
                $data['password'] = md5($pwd);
            }
            if($user->create()) {
                if($user->where('id='.$id)->save($data) !== false) {
                    $this->success('编辑用户成功',U('User/index'));
                }else {
                    $this->error('编辑用户失败',U('User/index'));
                }
            }else {
                $this->error($user->getError());
            }
        }else {
            //获取要编辑的数据
            $id = I('get.id',1,'int');
            $data = $user->where('id='.$id)->find();
            //添加管理员之前 查询出已有的角色
            $power = M('power');
            $power_data = $power->select();
            $this->assign('power_data',$power_data);
            $this->assign('data',$data);
						
						$campus = M('other')->where('pid=1')->select();
						$this->assign('campus',$campus);
            $this->display();
        }
    }
    
    /**
     * 删除管理员
     */
    public function del() {
        if(IS_POST) {
            $id = I('post.id',0,'int');
            //实例化管理员表
            $user = M('User');
            //获取登录的管理员id
            $my = session('qe_user.id');
            if($my == $id) {
                echo -2; //管理员不能删除自己
            }else {
                $data = $user->where('id='.$id.' and pwdid=1')->find();
                if($data) {
                   echo -3;  //不能删除超级管理员 
                }else {
                    if($user->where('id='.$id)->delete()) {
                        echo 1;
                    }else {
                        echo 0;
                    }  
                }

            }
        }else {
            echo -1;
        }
    }
	 public function login() {

        //实例化会员表
        $user = M('user');
        if(IS_POST) {
            $data['name'] = I('post.name','','addslashes');
            $data['password'] = md5(I('post.pwd','','addslashes'));
            $data2 = $user->where($data)->find();
            if($data2) {
							if($data2['quit_id'] == 1){
									session('qe_user',$data2);
									echo 1;exit;
							}else{
								echo -1;exit;
							}
            }else {
                echo 0;exit;
            }
        }else {
            $this->display();
        }
        
    }
    
    /**
     * 会员退出
     */
    public function logOut() {
        if(session('?qe_user')) {
            session('qe_user',null);
            $this->success('退出成功',U('User/login'));
        }
    }
		
		/**
		* 编辑管理员
		*/
		public function useredit() {
				//通过D实例化模型类
				$user = D('User');
				if(IS_POST) {
						$id = I('post.id',0,'int');
						$data['name'] = I('post.name','','addslashes');
						$pwd = I('post.pwd','','addslashes');
						if($pwd !='') {
								$data['password'] = md5($pwd);
						}
						if($user->create()) {
								if($user->where('id='.$id)->save($data) !== false) {
										$this->success('编辑用户成功',U('Index/index'));
								}else {
										$this->error('编辑用户失败',U('Index/index'));
								}
						}else {
								$this->error($user->getError());
						}
				}else {
						//获取要编辑的数据
						$id = session('qe_user')['id'];
						$data = $user->where('id='.$id)->find();
						//添加管理员之前 查询出已有的角色
						$power = M('power');
						$power_data = $power->select();
						$this->assign('power_data',$power_data);
						$this->assign('data',$data);
						
						$campus = M('other')->where('pid=1')->select();
						$this->assign('campus',$campus);
						$this->display();
				}
		}
		
}